package com.jokelandave.poc.server.dao;

import java.util.Date;
import java.util.List;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.Objectify;
import com.jokelandave.poc.server.domain.SportEvent;
import com.jokelandave.poc.server.domain.Team;

public class SportEventDao {

	Objectify obj;
	
	
	public SportEventDao(Objectify obj)
	{
		this.obj = obj;
	}
	
	public SportEvent getSportEventByKey(Key<SportEvent> sportEventKey)
	{
		return obj.get(sportEventKey);
	}
	
	/**
	 * Gets SportEvent list by team, only events that have not started yet 
	 * @param teamKey
	 * @return list of SportEvent
	 */
	public List<SportEvent> getSportEventCurrentByTeam(Key<Team> teamKey)
	{
		Date date = new Date();
		List<SportEvent> homeQuery = obj.query(SportEvent.class)
				.filter("homeTeam =", teamKey)
				.filter("eventStartTime >", date.getTime())
				.list();
		
		List<SportEvent> awayQuery = obj.query(SportEvent.class)
				.filter("awayTeam =", teamKey)
				.filter("eventStartTime >", date.getTime())
				.list();
		
		homeQuery.addAll(awayQuery);
		
		return homeQuery;
	}
	
	/**
	 * Gets SportEvent by team
	 * @param teamKey the key of the team to get
	 * @return a list of SportEvent
	 */
	public List<SportEvent> getAllSportEventsByTeam(Key<Team> teamKey)
	{
		List<SportEvent> homeQuery = obj.query(SportEvent.class)
				.filter("homeTeam =", teamKey)
				.list();
		
		List<SportEvent> awayQuery = obj.query(SportEvent.class)
				.filter("awayTeam =", teamKey)
				.list();
		
		homeQuery.addAll(awayQuery);
		
		return homeQuery;
	}
}
